package cn.vihub.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.vihub.param.ShopUserParam;
import cn.vihub.service.ShopMenuService;
import cn.vihub.service.ShopRoleService;
import cn.vihub.vo.ShopUserVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.vihub.mapper.ShopUserMapper;
import cn.vihub.pojo.ShopUser;
import cn.vihub.service.ShopUserService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author: 秋晚
 * @Date:  2025/7/18 16:14
 * @Description: 
 **/

@Service
@Transactional(rollbackFor = Exception.class)
public class ShopUserServiceImpl extends ServiceImpl<ShopUserMapper, ShopUser> implements ShopUserService{

    @Autowired
    ShopRoleService shopRoleService;

    @Autowired
    ShopMenuService shopMenuService;

    @Override
    public IPage<ShopUser> selectPage(ShopUserParam shopUserParam) {
        //创建分页对象
        Page<ShopUser> userPage = new Page<>(shopUserParam.getPage(), shopUserParam.getLimit());

        //创建lambda表达式
        LambdaQueryWrapper<ShopUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //先用工具类判断用户名是否为空  不为空则模糊查询
        lambdaQueryWrapper.like(StringUtils.isNotBlank(shopUserParam.getUsername()),
                ShopUser::getUsername,shopUserParam.getUsername());
        //使用lambda表达式根据用户主键ID进行倒序排序  确保刚添加的用户在第一条
//        lambdaQueryWrapper.orderByDesc(ShopUser::getId);
        //orderBy方法  orderBy(boolean condition, boolean isAsc, R... columns)  condition表示是否应用该排序  isAsc表示是升序还是降序  columns可变参数列表 根据哪个字段排序
        lambdaQueryWrapper.orderBy(StringUtils.isNotBlank(shopUserParam.getOrderColumn()),
                "ascending".equals(shopUserParam.getOrderType()),ShopUser::getId);
        //返回数据
        return page(userPage,lambdaQueryWrapper);
    }

    @Override
    public Boolean save(ShopUserParam shopUserParam) {

        if(ObjectUtil.isEmpty(shopUserParam.getId())){
            shopUserParam.setCreateTime(new Date());
        }else{
            shopUserParam.setUpdateTime(new Date());
        }
        //创建一个新的对象用于存放数据
        ShopUser shopUser = new ShopUser();
        //使用拷贝赋值
        BeanUtils.copyProperties(shopUserParam,shopUser);

        if(StringUtils.isNotBlank(shopUserParam.getPassword())){
            //密码不为空就加密存储
            String s = SecureUtil.md5(shopUserParam.getPassword());
            shopUser.setPassword(s);
        }
        //使用mybatisPlus中的接口进行新增或编辑
        return saveOrUpdate(shopUser);
    }

    @Override
    public ShopUserVo getUserDetail(Integer userId) {
        //获取用户详情
        ShopUser shopUser = getById(userId);
        ShopUserVo shopUserVo = new ShopUserVo();
        shopUserVo.setId(shopUser.getId());
        shopUserVo.setUsername(shopUser.getUsername());
        shopUserVo.setRoleList(shopRoleService.getRoleByUserId(userId));
        shopUserVo.setTreeList(shopMenuService.getTreeByUserId(userId));
        return shopUserVo;
    }
}
